package com.pingtai.indent.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Session;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.pingtai.dao.AbstractManager;
import com.pingtai.indent.dao.IndentDao;
import com.pingtai.indent.entity.IndentNumberTmp;
import com.pingtai.indent.entity.IndentQqPlzb;
import com.pingtai.indent.entity.IndentQqPlzbType;
import com.pingtai.indent.entity.IndentRebateRelation;
import com.pingtai.indent.entity.IndentXinlangZb;
import com.pingtai.indent.entity.IndentXinlangZbType;
import com.pingtai.indent.entity.Qq;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndent;
import com.pingtai.indent.entity.UserIndentMoneyTmp;
import com.pingtai.indent.entity.UserIndentSearch;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.indent.entity.XinlangPersonRenzheng;
import com.pingtai.indent.entity.XinlangPrimaryFanStatistics;
import com.pingtai.user.entity.User;
import com.pingtai.user.entity.UserPay;
import com.pingtai.user.entity.UserSearch;
import com.pingtai.user.service.UserManagerService;
import com.pingtai.util.PaginationSupport;
import com.pingtai.util.Utils;

public class WeiboIndentService {

	private IndentDao indentDao;
	private AbstractManager abstractManager;
	private UserManagerService userManagerService;
	
	
	public UserManagerService getUserManagerService() {
		return userManagerService;
	}



	public void setUserManagerService(UserManagerService userManagerService) {
		this.userManagerService = userManagerService;
	}



	public AbstractManager getAbstractManager() {
		return abstractManager;
	}



	public void setAbstractManager(AbstractManager abstractManager) {
		this.abstractManager = abstractManager;
	}



	public IndentDao getIndentDao() {
		return indentDao;
	}



	public void setIndentDao(IndentDao indentDao) {
		this.indentDao = indentDao;
	}


	public PaginationSupport getIndentForPage(int pageName,boolean flag,List baseList) {
		Session session = indentDao.getSession();
		User cUser = userManagerService.findCurrectUser();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		if(flag){
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
//				if(!userManagerService.isRoot(cUser)){
//					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
//					if(t==null) continue;
//				}
				
				dis.add(Restrictions.eq("type", base)); 
			}
			
			criteria.add(dis).addOrder(
					Order.desc("createDate"));
		}else{
			
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
				if(!userManagerService.isRoot(cUser)){
					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
					if(t==null) continue;
				}
				dis.add(Restrictions.eq("type", base));  
			}
			
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(dis).addOrder(
					Order.desc("createDate"));
		}
		
		if(userManagerService.isAudit(cUser)){
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getParentUserId()+":",MatchMode.ANYWHERE));
		}else{
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getUserId().toString()+":",MatchMode.ANYWHERE));
		}
		
		
		//criteria.createAlias("user", "u").add(Restrictions.like("u.parentUserIdString", cUser.getUserId().toString(),MatchMode.ANYWHERE));
		PaginationSupport ps = new PaginationSupport();
		
		ProjectionList projectionList = Projections.projectionList();
    	projectionList.add(Projections.groupProperty("state"));
    	projectionList.add(Projections.rowCount());
    	criteria.setProjection(projectionList);
    	List indents = criteria.list();
    	Iterator iterator = indents.iterator();
    	while(iterator.hasNext()) {
    	    Object[] o = (Object[]) iterator.next();
    	    if(o[0].equals(0)){
    	    	ps.setShenheCount((Long)o[1]);
    	    }else if(o[0].equals(1)){
    	    	ps.setZhixingCount((Long)o[1]);
    	    }else if(o[0].equals(2)){
    	    	ps.setWanchengCount((Long)o[1]);
    	    }else if(o[0].equals(3)){
    	    	ps.setTuikuanCount((Long)o[1]);
    	    }else if(o[0].equals(4)){
    	    	ps.setStateCount4((Long)o[1]);
    	    }else if(o[0].equals(5)){
    	    	ps.setStateCount5((Long)o[1]);
    	    }else if(o[0].equals(6)){
    	    	ps.setStateCount6((Long)o[1]);
    	    }
    	}
    	
    	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    	criteria.setProjection(null);
	
    	
    	
    	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
    	List items = ps2.getItems();
    	
    	//items = removeIndentNoCurAndChild(items,cUser);
    	ps.setItems(items);
    	ps.setIndexes(ps2.getIndexes());
    	ps.setPageNum(ps2.getPageNum());
    	ps.setPageSize(ps2.getPageSize());
    	ps.setStartIndex(ps2.getStartIndex());
    	ps.setTotalCount(totalCount);
    	return ps;
	}
	
	public List findAllUserIndentTypeNoParent(User user){
		return indentDao.findAllUserIndentTypeNoParent(user);
	}
	
	public Long getUserIndentNumberByStateAndBase(User user,Integer state,UserBaseIndentType base){
		Long number = 0l;
		number += indentDao.getUserIndentNumberByStateAndBase(user,state,base);
		return number;
	}
	

	public PaginationSupport getIndentForPage(int pageName,boolean flag,int base) {
		Session session = indentDao.getSession();
		User cUser = userManagerService.findCurrectUser();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		if(flag){
			criteria.add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}else{
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}
		
		if(userManagerService.isAudit(cUser)){
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getParentUserId()+":",MatchMode.ANYWHERE));
		}else{
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getUserId().toString()+":",MatchMode.ANYWHERE));
		}
		//criteria.createAlias("user", "u").add(Restrictions.like("u.parentUserIdString", cUser.getUserId().toString(),MatchMode.ANYWHERE));
		PaginationSupport ps = new PaginationSupport();
		
		ProjectionList projectionList = Projections.projectionList();
    	projectionList.add(Projections.groupProperty("state"));
    	projectionList.add(Projections.rowCount());
    	criteria.setProjection(projectionList);
    	List indents = criteria.list();
    	Iterator iterator = indents.iterator();
    	while(iterator.hasNext()) {
    	    Object[] o = (Object[]) iterator.next();
    	    if(o[0].equals(0)){
    	    	ps.setShenheCount((Long)o[1]);
    	    }else if(o[0].equals(1)){
    	    	ps.setZhixingCount((Long)o[1]);
    	    }else if(o[0].equals(2)){
    	    	ps.setWanchengCount((Long)o[1]);
    	    }else if(o[0].equals(3)){
    	    	ps.setTuikuanCount((Long)o[1]);
    	    }else if(o[0].equals(4)){
    	    	ps.setStateCount4((Long)o[1]);
    	    }else if(o[0].equals(5)){
    	    	ps.setStateCount5((Long)o[1]);
    	    }else if(o[0].equals(6)){
    	    	ps.setStateCount6((Long)o[1]);
    	    }
    	}
    	
    	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    	criteria.setProjection(null);
	
    	
    	
    	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
    	List items = ps2.getItems();
    	
    	//items = removeIndentNoCurAndChild(items,cUser);
    	ps.setItems(items);
    	ps.setIndexes(ps2.getIndexes());
    	ps.setPageNum(ps2.getPageNum());
    	ps.setPageSize(ps2.getPageSize());
    	ps.setStartIndex(ps2.getStartIndex());
    	ps.setTotalCount(totalCount);
    	return ps;
	}
	
	private List removeIndentNoCurAndChild(List items,User cUser){
		for(int i=0;i<items.size();i++){
			UserIndent indent = (UserIndent) items.get(i);
			User user = indent.getUser();
			if(!userManagerService.isChildren(cUser, user)){
				items.remove(i);
				i--;
			}
		}
		return items;
	}
	
	public List getUserConsumptionTotalMoney(User user){
		return indentDao.getUserConsumptionTotalMoney(user);
	}
	
	public PaginationSupport getUserConsumptionForPage(int pageName,User user){
		Session session = indentDao.getSession();
		Criteria criteria = session.createCriteria(UserIndentMoneyTmp.class);
		criteria.add(Restrictions.eq("user", user));
		criteria.createCriteria("userIndent").addOrder(Order.desc("createDate"));
		PaginationSupport ps = new PaginationSupport();
		
		ProjectionList projectionList = Projections.projectionList();
    	projectionList.add(Projections.rowCount());
    	criteria.setProjection(projectionList);
    	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    	criteria.setProjection(null);
	
    	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
    	
    	List items = ps2.getItems();
    	
    	ps.setItems(items);
    	
    	ps.setIndexes(ps2.getIndexes());
    	ps.setPageNum(ps2.getPageNum());
    	ps.setPageSize(ps2.getPageSize());
    	ps.setStartIndex(ps2.getStartIndex());
    	ps.setTotalCount(totalCount);
    	
    	List list = getUserConsumptionTotalMoney(user);
    	Float money = 0f;
    	if(list!=null&&list.size()>0){
    		for(int i=0;i<list.size();i++){
    			UserIndentMoneyTmp moneyTmp = (UserIndentMoneyTmp) list.get(i);
    			UserIndent indent = moneyTmp.getUserIndent();
    			UserBaseIndentType base = indent.getBaseIndentType();
    			if(base.getStateType().equals("default")&&!indent.getState().equals(3)){
    				money += indent.getTotalPrice();
    			}else if(base.getStateType().equals("renzheng")&&!indent.getState().equals(5)){
    				money += indent.getTotalPrice();
    			}
    		}
    	}
    	ps.setTotalCosumption(money);
    	return ps;
	}
	
	public PaginationSupport getIndentForPage(int pageName,boolean flag,List baseList,UserIndentSearch qe) {
		Session session = indentDao.getSession();
		User cUser = userManagerService.findCurrectUser();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		
		if(flag){
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
//				if(!userManagerService.isRoot(cUser)){
//					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
//					if(t==null) continue;
//				}
				dis.add(Restrictions.eq("type", base));  
			}
			
			criteria.add(dis).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}else{
			
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
				if(!userManagerService.isRoot(cUser)){
					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
					if(t==null) continue;
				}
				dis.add(Restrictions.eq("type", base));  
			}
			
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(dis).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}
		
		
		if(userManagerService.isAudit(cUser)){
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getParentUserId()+":",MatchMode.ANYWHERE));
		}else{
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getUserId().toString()+":",MatchMode.ANYWHERE));
		}
		//criteria.createAlias("user", "u").add(Restrictions.like("u.parentUserIdString", cUser.getUserId().toString(),MatchMode.ANYWHERE));
		if(qe!=null){
			
			if(qe.getBianhao()!=null&&!qe.getBianhao().isEmpty()){
				criteria = criteria.add(Restrictions.eq("id", new Integer(qe.getBianhao())));
			}
			
			if(qe.getWeiboUrl()!=null &&!qe.getWeiboUrl().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getWeiboUrl(),MatchMode.ANYWHERE));
			}
			
			if(qe.getWeiboTitle()!=null &&!qe.getWeiboTitle().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetTitle", qe.getWeiboTitle(),MatchMode.ANYWHERE));
			}
			
			if(qe.getUid()!=null && !qe.getUid().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getUid(),MatchMode.ANYWHERE));
			}
			if(qe.getWeixinNumber()!=null && !qe.getWeixinNumber().isEmpty()){
				criteria = criteria.add(Restrictions.like("weixinNum", qe.getWeixinNumber(),MatchMode.ANYWHERE));
			}
			
		}
		criteria.addOrder(
				Order.desc("createDate"));
		if(qe!=null){
			if(qe.getWeiboId()!=null && !qe.getWeiboId().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("weiboId", qe.getWeiboId(),MatchMode.ANYWHERE));
			}
			if(qe.getName()!=null && !qe.getName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("name", qe.getName(),MatchMode.ANYWHERE));
			}
			if(qe.getTrueName()!=null && !qe.getTrueName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("trueName", qe.getTrueName(),MatchMode.ANYWHERE));
			}
			if(qe.getIdCard()!=null && !qe.getIdCard().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("idCard", qe.getIdCard(),MatchMode.ANYWHERE));
			}
			if(qe.getTel()!=null && !qe.getTel().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("telNumber", qe.getTel(),MatchMode.ANYWHERE));
			}
		}
		
		PaginationSupport ps = new PaginationSupport();
		
    		ProjectionList projectionList = Projections.projectionList();
        	projectionList.add(Projections.groupProperty("state"));
        	projectionList.add(Projections.rowCount());
        	criteria.setProjection(projectionList);
        	List indents = criteria.list();
        	Iterator iterator = indents.iterator();
        	while(iterator.hasNext()) {
        	    Object[] o = (Object[]) iterator.next();
        	    if(o[0].equals(0)){
        	    	ps.setShenheCount((Long)o[1]);
        	    }else if(o[0].equals(1)){
        	    	ps.setZhixingCount((Long)o[1]);
        	    }else if(o[0].equals(2)){
        	    	ps.setWanchengCount((Long)o[1]);
        	    }else if(o[0].equals(3)){
        	    	ps.setTuikuanCount((Long)o[1]);
        	    }else if(o[0].equals(4)){
        	    	ps.setStateCount4((Long)o[1]);
        	    }else if(o[0].equals(5)){
        	    	ps.setStateCount5((Long)o[1]);
        	    }else if(o[0].equals(6)){
        	    	ps.setStateCount6((Long)o[1]);
        	    }
        	}
        	
        	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
        	criteria.setProjection(null);
    	
        	if(qe!=null){
    			
    			if (qe.getStatus()!= null&&!qe.getStatus().equalsIgnoreCase("all")) {
    				criteria = criteria.add(Restrictions.eq("state", new Integer(qe.getStatus())));
    			}
        	}
        	
        	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
        	
        	List items = ps2.getItems();
        	
        	//items = removeIndentNoCurAndChild(items,cUser);
        	ps.setItems(items);
        	
        	ps.setItems(items);
        	ps.setIndexes(ps2.getIndexes());
        	ps.setPageNum(ps2.getPageNum());
        	ps.setPageSize(ps2.getPageSize());
        	ps.setStartIndex(ps2.getStartIndex());
        	ps.setTotalCount(totalCount);
		return ps;
	}
	
	
	public PaginationSupport getIndentForPage(int pageName,boolean flag,int base,UserIndentSearch qe) {
		Session session = indentDao.getSession();
		User cUser = userManagerService.findCurrectUser();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		if(flag){
			criteria.add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0));
		}else{
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0));
		}
		if(userManagerService.isAudit(cUser)){
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getParentUserId()+":",MatchMode.ANYWHERE));
		}else{
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getUserId().toString()+":",MatchMode.ANYWHERE));
		}
		//criteria.createAlias("user", "u").add(Restrictions.like("u.parentUserIdString", cUser.getUserId().toString(),MatchMode.ANYWHERE));
		if(qe!=null){
			
			if(qe.getBianhao()!=null&&!qe.getBianhao().isEmpty()){
				criteria = criteria.add(Restrictions.eq("id", new Integer(qe.getBianhao())));
			}
			
			if(qe.getWeiboUrl()!=null &&!qe.getWeiboUrl().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getWeiboUrl(),MatchMode.ANYWHERE));
			}
			
			if(qe.getWeiboTitle()!=null &&!qe.getWeiboTitle().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetTitle", qe.getWeiboTitle(),MatchMode.ANYWHERE));
			}
			
			if(qe.getUid()!=null && !qe.getUid().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getUid(),MatchMode.ANYWHERE));
			}
			if(qe.getWeixinNumber()!=null && !qe.getWeixinNumber().isEmpty()){
				criteria = criteria.add(Restrictions.like("weixinNum", qe.getWeixinNumber(),MatchMode.ANYWHERE));
			}
			
		}
		criteria.addOrder(
				Order.desc("createDate"));
		if(qe!=null){
			if(qe.getWeiboId()!=null && !qe.getWeiboId().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("weiboId", qe.getWeiboId(),MatchMode.ANYWHERE));
			}
			if(qe.getName()!=null && !qe.getName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("name", qe.getName(),MatchMode.ANYWHERE));
			}
			if(qe.getTrueName()!=null && !qe.getTrueName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("trueName", qe.getTrueName(),MatchMode.ANYWHERE));
			}
			if(qe.getIdCard()!=null && !qe.getIdCard().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("idCard", qe.getIdCard(),MatchMode.ANYWHERE));
			}
			if(qe.getTel()!=null && !qe.getTel().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria("tencentPersonRenzheng").add(Restrictions.like("telNumber", qe.getTel(),MatchMode.ANYWHERE));
			}
		}
		
		PaginationSupport ps = new PaginationSupport();
		
    		ProjectionList projectionList = Projections.projectionList();
        	projectionList.add(Projections.groupProperty("state"));
        	projectionList.add(Projections.rowCount());
        	criteria.setProjection(projectionList);
        	List indents = criteria.list();
        	Iterator iterator = indents.iterator();
        	while(iterator.hasNext()) {
        	    Object[] o = (Object[]) iterator.next();
        	    if(o[0].equals(0)){
        	    	ps.setShenheCount((Long)o[1]);
        	    }else if(o[0].equals(1)){
        	    	ps.setZhixingCount((Long)o[1]);
        	    }else if(o[0].equals(2)){
        	    	ps.setWanchengCount((Long)o[1]);
        	    }else if(o[0].equals(3)){
        	    	ps.setTuikuanCount((Long)o[1]);
        	    }else if(o[0].equals(4)){
        	    	ps.setStateCount4((Long)o[1]);
        	    }else if(o[0].equals(5)){
        	    	ps.setStateCount5((Long)o[1]);
        	    }else if(o[0].equals(6)){
        	    	ps.setStateCount6((Long)o[1]);
        	    }
        	}
        	
        	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
        	criteria.setProjection(null);
    	
        	if(qe!=null){
    			
    			if (qe.getStatus()!= null&&!qe.getStatus().equalsIgnoreCase("all")) {
    				criteria = criteria.add(Restrictions.eq("state", new Integer(qe.getStatus())));
    			}
        	}
        	
        	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
        	
        	List items = ps2.getItems();
        	
        	//items = removeIndentNoCurAndChild(items,cUser);
        	ps.setItems(items);
        	
        	ps.setItems(items);
        	ps.setIndexes(ps2.getIndexes());
        	ps.setPageNum(ps2.getPageNum());
        	ps.setPageSize(ps2.getPageSize());
        	ps.setStartIndex(ps2.getStartIndex());
        	ps.setTotalCount(totalCount);
		return ps;
	}
	
	public UserIndentSearch paseQueryString(UserIndentSearch qe, String queryString) throws ParseException{
		Map param = Utils.URLRequest(queryString);
		if(param.get("qe.bianhao")!=null){
			String bianhao = (String) param.get("qe.bianhao");
			if(!bianhao.isEmpty()){
				qe.setBianhao(bianhao);
			}
		}
		
		if(param.get("qe.weiboUrl")!=null){
			String weiboUrl = (String) param.get("qe.weiboUrl");
			if(!weiboUrl.isEmpty()){
				qe.setWeiboUrl(weiboUrl);
			}
		}
		
		if(param.get("qe.uid")!=null){
			String uid = (String) param.get("qe.uid");
			if(!uid.isEmpty()){
				qe.setUid(uid);
			}
		}
		return qe;
	}
	
	public List getIndentBaseTypeOfLevel3(Integer base){
		List list = new ArrayList();
		UserBaseIndentType indentBase = (UserBaseIndentType) indentDao.getObject(UserBaseIndentType.class, base);
		List child = findAllChildBaseIndentType(indentBase);
		if(child!=null&&child.size()>0){
			for(int i=0;i<child.size();i++){
				UserBaseIndentType c = (UserBaseIndentType) child.get(i);
				List child2 = findAllChildBaseIndentType(c);
				if(child2!=null&&child2.size()>0){
					List l = getIndentBaseTypeOfLevel3(c.getId());
					list.addAll(l);
				}else{
					list.add(c);
				}
			}
		}else{
			list.add(indentBase);
		}
		return list;
	}
	
	public UserBaseIndentType getBaseIndentTypeById(int base){
		return (UserBaseIndentType) indentDao.getObject(UserBaseIndentType.class, base);
	}
	
	public List getAllIndentNumIncrementList(String date,UserBaseIndentType baseIndentType) throws ParseException{
		List childBaseIndentTypeList = findAllChildBaseIndentType(baseIndentType);
		List list = new ArrayList();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		Date startDate = format.parse(date + " 00:00:00");
        Date endDate = format.parse(date + " 23:59:59");
        List stateList = new ArrayList();
        stateList.add(new Integer(1));
        stateList.add(new Integer(2));
        if(childBaseIndentTypeList!=null&&childBaseIndentTypeList.size()>0){
        	for(int i=0;i<childBaseIndentTypeList.size();i++){
				UserBaseIndentType baseIndent = (UserBaseIndentType) childBaseIndentTypeList.get(i);
				List indentList = getAllIndentByBase(baseIndent,stateList);
				for(int j=0;j<indentList.size();j++){
					UserIndent indent = (UserIndent) indentList.get(j);
					XinlangPrimaryFanStatistics statis = new XinlangPrimaryFanStatistics();
					statis.setIndent(indent);
					statis = getIndentIncrement(startDate,endDate,statis);
					if(statis==null) continue;
					list.add(statis);
				}
        	}
        	
        }else{
        	List indentList = getAllIndentByBase(baseIndentType,stateList);
			for(int j=0;j<indentList.size();j++){
				UserIndent indent = (UserIndent) indentList.get(j);
				XinlangPrimaryFanStatistics statis = new XinlangPrimaryFanStatistics();
				statis.setIndent(indent);
				statis = getIndentIncrement(startDate,endDate,statis);
				list.add(statis);
			}
        }
        
        return list;
	}
	
	public XinlangPrimaryFanStatistics getIndentIncrement(Date start,Date end,XinlangPrimaryFanStatistics statis){
		UserIndent indent = statis.getIndent();
		List list = indentDao.getIndentcrement(start,end,indent);
		Integer num = 0;
		if(list!=null&&list.size()>0){
			IndentNumberTmp firstNumTmp = (IndentNumberTmp) list.get(0);
			IndentNumberTmp endNumTmp = (IndentNumberTmp) list.get(list.size()-1);
			num = endNumTmp.getNumber() - firstNumTmp.getNumber();
		}
		statis.setTotalFanNum(num);
		
		if(num==0) return null;
		return statis;
	}
	
	public void deleteAllIndentByBase(List baseList,Integer state,Integer day){
		indentDao.deleteAllIndentByBase(baseList, state, day);
	}
	
	public List getAllIndentByBase(UserBaseIndentType base,List stateList){
		return indentDao.getAllIndentByBase(base,stateList);
	}
	
	public List getAllIndentOfCreateByDate(String date ,UserBaseIndentType baseIndentType){
		List childBaseIndentTypeList = findAllChildBaseIndentType(baseIndentType);
		List list = new ArrayList();
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date startDate = format.parse(date + " 00:00:00");
            Date endDate = format.parse(date + " 23:59:59");
		if(childBaseIndentTypeList!=null&&childBaseIndentTypeList.size()>0){
			for(int i=0;i<childBaseIndentTypeList.size();i++){
				UserBaseIndentType baseIndent = (UserBaseIndentType) childBaseIndentTypeList.get(i);
				List childBaseIndentTypeList2 = findAllChildBaseIndentType(baseIndent);
				if(childBaseIndentTypeList2!=null&&childBaseIndentTypeList2.size()>0){
					for(int j=0;j<childBaseIndentTypeList2.size();j++){
						UserBaseIndentType baseIndent2 = (UserBaseIndentType) childBaseIndentTypeList2.get(j);
						List l2 = indentDao.getAllIndentOfCreateByDate(startDate,endDate,baseIndent2);
						list.addAll(l2);
					}
				}
				
				List l = indentDao.getAllIndentOfCreateByDate(startDate,endDate,baseIndent);
				list.addAll(l);
			}
		}else{
			list = indentDao.getAllIndentOfCreateByDate(startDate,endDate,baseIndentType);
		}
		  } 
		catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return list;
	}
	
	public List getAllIndentOfCompletedByDate(String date,UserBaseIndentType baseIndentType){
		List childBaseIndentTypeList = findAllChildBaseIndentType(baseIndentType);
		List list = new ArrayList();
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            Date startDate = format.parse(date + " 00:00:00");
            Date endDate = format.parse(date + " 23:59:59");
		if(childBaseIndentTypeList!=null&&childBaseIndentTypeList.size()>0){
			for(int i=0;i<childBaseIndentTypeList.size();i++){
				UserBaseIndentType baseIndent = (UserBaseIndentType) childBaseIndentTypeList.get(i);
				List l = indentDao.getAllIndentOfCompletedByDate(startDate,endDate,baseIndent);
				list.addAll(l);
			}
		}else{
			list = indentDao.getAllIndentOfCompletedByDate(startDate,endDate,baseIndentType);
		}
		  } 
		catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return list;
	}
	
	public Integer getWeixinSanmeNum(String weixinNum) throws ParseException{
		return indentDao.getWeixinSanmeNum(weixinNum);
	}
	
	public UserIndentType getUserIndentTypeByUserAndType(User user,int id){
		List list = indentDao.findUserIndentTypeByUserAndType(user,id);
		if(list!=null&&list.size()>0){
			return (UserIndentType) list.get(0);
		}
		return null;
	}
	
	public UserIndentType updateIndentType(UserIndentType indentType){
		UserBaseIndentType baseIndentType = indentType.getUserBaseIndentType();
		Date indentTypeUpdateDate = indentType.getUpdateDate();
		Date baseIndentTypeUpdateDate = baseIndentType.getUpdateDate();
		Float t = 0f;
		Float defaultPrice = indentType.getDefaultPrice();
		Float price = indentType.getPrice();
		if(baseIndentTypeUpdateDate.getTime()>indentTypeUpdateDate.getTime()){
			Float base_basePrice = baseIndentType.getPrice();
			Float old_basePrice = indentType.getBasePrice();
			
			t = old_basePrice-base_basePrice;
			price -=t;
			if(defaultPrice!=null){
				defaultPrice -=t;
			}
		}
		
		indentType.setPrice(price);
		indentType.setDefaultPrice(defaultPrice);
		indentType.setBasePrice(baseIndentType.getPrice());
		indentType.setUpdateDate(baseIndentTypeUpdateDate);
		UserIndentType type =  (UserIndentType) indentDao.update(indentType);
		User curUser = type.getUser();
		if(!userManagerService.isAdmin(curUser)){
			User parentUser = userManagerService.findUserById(curUser.getParentUserId());
			UserIndentType p_type = getUserIndentTypeByUserAndType(parentUser, baseIndentType.getId());
			updateIndentType(p_type);
		}
		return type;
	}
	
	public boolean isLawfulOfPrice(Float price ,User user,int type){
		
		List list = indentDao.findUserIndentTypeByUserAndType(user, type);
		
		if(list==null||list.size()==0){
			if(!userManagerService.isRoot(user)){
				User u = userManagerService.findUserById(user.getParentUserId());
				return isLawfulOfPrice(price, u, type);
			}
			UserBaseIndentType base = (UserBaseIndentType) indentDao.getObject(UserBaseIndentType.class, type);
			Float tmp = base.getPrice();
			if(price < tmp){
				return false;
			}else{
				return true;
			}
		}else{
			UserIndentType userIndentType = (UserIndentType) list.get(0);
			Float tmp = userIndentType.getPrice();
			if(price<tmp){
				return false;
			}else{
				if(!userManagerService.isRoot(user)){
					User parentUser = userManagerService.findUserById(user.getParentUserId());
					return isLawfulOfPrice(price,parentUser,type);
				}
			}
		}
		
		return false;
	}
	
	public UserIndent saveUserIndent(UserIndent userIndent){
		indentDao.saveOrUpdate(userIndent);
		return userIndent;
	}
	
	public UserIndent updateUserIndentForCompleate(UserIndent userIndent){
		indentDao.update(userIndent);
		return userIndent;
	}
	
	
	
	public UserIndentMoneyTmp saveUserIndentMoneyTmp(UserIndentMoneyTmp userIndentMoneyTmp){
		return (UserIndentMoneyTmp) indentDao.save(userIndentMoneyTmp);
	}
	
	public void saveUserIndentRebateRelation(UserIndent userIndent,
			UserBaseIndentType baseIndentType,int sort,UserIndentType indentType,
			User user,Float price,User childUser,Float childPrice) {
		sort +=1;
		IndentRebateRelation indentRebateRelation = new IndentRebateRelation();
		indentRebateRelation.setNumber(userIndent.getNumber());
		indentRebateRelation.setUserIndent(userIndent);
		indentRebateRelation.setSort(sort);
		indentRebateRelation.setUserBaseIndentType(baseIndentType);
		indentRebateRelation.setUser(user);
		indentRebateRelation.setPrice(price);
		if(childUser!=null){
			UserIndentType childIndentType = getUserIndentTypeByUserAndType(childUser,baseIndentType.getId());
			if(childIndentType!=null){
				indentRebateRelation.setChildPrice(childIndentType.getPrice());
			}else{
				indentRebateRelation.setChildPrice(childPrice);
			}
			
			Float rebate = indentRebateRelation.getChildPrice()*indentRebateRelation.getNumber()/baseIndentType.getUnit2()-
					indentRebateRelation.getPrice()*indentRebateRelation.getNumber()/baseIndentType.getUnit2();
			indentRebateRelation.setRebateMoney(rebate);
		}else{
			indentRebateRelation.setRebateMoney(0.0F);
		}
		
		indentDao.save(indentRebateRelation);
		if(user.getParentUserId()!=null){
			User parentUser = userManagerService.findUserById(user.getParentUserId());
			UserIndentType parentIndentType = getUserIndentTypeByUserAndType(parentUser,baseIndentType.getId());
			Float parentPrice=0f;
			if(parentIndentType==null){
				parentPrice = price;
			}else{
				parentPrice = parentIndentType.getPrice();
			}
			saveUserIndentRebateRelation(userIndent,baseIndentType,sort,parentIndentType,
					parentUser,parentPrice,user,price);
		}
	}
	
	public UserIndent getUserIndentById(Integer id){
		return (UserIndent) indentDao.getObject(UserIndent.class, id);
	}
	
	public void updateUserIndent(UserIndent userIndent){
		if(userIndent.getRenzheng()!=null){
			if(userIndent.getRenzheng().getXinlangPersonRenzheng()!=null){
				userIndent.getRenzheng().getXinlangPersonRenzheng().setState(userIndent.getState());
			}
			if(userIndent.getRenzheng().getTencentBusinessRenzheng()!=null){
				userIndent.getRenzheng().getTencentBusinessRenzheng().setState(userIndent.getState());
			}
			if(userIndent.getRenzheng().getTencentPersonRenzheng()!=null){
				userIndent.getRenzheng().getTencentPersonRenzheng().setState(userIndent.getState());
			}
		}
		indentDao.update(userIndent);
	}
	
	public int batchAddUserIndentTypeBasePrice(){
		List list = indentDao.getAllUserIndentType();
		for(int i=0;i<list.size();i++){
			UserIndentType indentType = (UserIndentType) list.get(i);
			UserBaseIndentType base = indentType.getUserBaseIndentType();
			indentType.setBasePrice(base.getPrice());
			indentType.setUpdateDate(base.getUpdateDate());
			indentDao.update(indentType);
		}
		return 1;
		
	}
	
	public int batchAddBaseIndentTypeUpdateDate(){
		List list = indentDao.getAllBaseIndentType();
		for(int i=0;i<list.size();i++){
			UserBaseIndentType base = (UserBaseIndentType) list.get(i);
			base.setUpdateDate(new Date());
			indentDao.update2(base);
		}
		return 1;
		
	}
	
	public List getAllRuningIndent(int base){
		UserBaseIndentType baseIndentType = (UserBaseIndentType) indentDao.getObject(UserBaseIndentType.class, base);
		List childBaseIndentTypeList = findAllChildBaseIndentType(baseIndentType);
		List list = new ArrayList();
		if(childBaseIndentTypeList!=null&&childBaseIndentTypeList.size()>0){
			for(int i=0;i<childBaseIndentTypeList.size();i++){
				UserBaseIndentType baseIndent = (UserBaseIndentType) childBaseIndentTypeList.get(i);
				List l = indentDao.findAllRuningIndent( baseIndent.getId());
				list.addAll(l);
			}
		}else{
			list =  indentDao.findAllRuningIndent( base);
		}
		return list;
	}
	
	public void updateIndentPay(){
		indentDao.updateIndentPay();
	}
	
	public List getAllRuningIndent2(int base){
		UserBaseIndentType baseIndentType = (UserBaseIndentType) indentDao.getObject(UserBaseIndentType.class, base);
		List childBaseIndentTypeList = findAllChildBaseIndentType(baseIndentType);
		List list = new ArrayList();
		if(childBaseIndentTypeList!=null&&childBaseIndentTypeList.size()>0){
			for(int i=0;i<childBaseIndentTypeList.size();i++){
				UserBaseIndentType baseIndent = (UserBaseIndentType) childBaseIndentTypeList.get(i);
				List l = indentDao.findAllRuningIndent( baseIndent.getId());
				list.addAll(l);
			}
			List l = indentDao.findAllRuningIndent( base);
			list.addAll(l);
		}else{
			list =  indentDao.findAllRuningIndent( base);
		}
		return list;
	}
	
	public List findAllChildBaseIndentType(UserBaseIndentType baseIndentType){
		return indentDao.findAllChildBaseIndentType(baseIndentType);
	}
	
	public List getAllIndentRebateRelation(User user,int state){
		return indentDao.getAllIndentRebateRelation( user, state);
	}
	
	public List getAllIndentRebateRelation(User user,int state,Date begin,Date end){
		return indentDao.getAllIndentRebateRelation( user, state,begin,end);
	}
	
	
	public Float getAllRebateMoneyOfChildren(User user,Float money,int state){
			List rebateList = getAllIndentRebateRelation(user,state);
			for(int j=0;j<rebateList.size();j++){
				IndentRebateRelation rebate = (IndentRebateRelation) rebateList.get(j);
				money += rebate.getRebateMoney();
			}
		return money;
	}
	
	public Float getAllRebateMoneyOfChildren(User user,Float money,int state,Date begin,Date end){
			List rebateList = getAllIndentRebateRelation(user,state,begin,end);
			for(int j=0;j<rebateList.size();j++){
				IndentRebateRelation rebate = (IndentRebateRelation) rebateList.get(j);
				money += rebate.getRebateMoney();
			}
		return money;
	}
	
	public void saveIndentQq(String q,UserIndent userIndent){
		Qq qq = new Qq();
		qq.setQq(q);
		qq.setUserIndent(userIndent);
		indentDao.save(qq);
	}
	
	public String getQqNumber(Integer indentId){
		Qq qq = indentDao.getQqByIndentId(indentId);
		if(qq!=null) return qq.getQq();
		return null;
	}
	
	public void saveIndentNumberTmp(IndentNumberTmp indentNumberTmp){
		indentDao.save(indentNumberTmp);
	}
	
	public List getIndentNumberTmp(UserIndent userIndent){
		return indentDao.getIndentNumberTmp(userIndent);
	}
	
	public List getQqPlzbTypeList(){
		return indentDao.getQqPlzbTypeList();
	}
	
	public List getXinlangZbTypeList(){
		return indentDao.getXinlangZbTypeList();
	}
	
	public IndentQqPlzbType getQqPlzbTypeById(Integer id){
		return (IndentQqPlzbType) indentDao.getObject(IndentQqPlzbType.class, id);
	}
	
	public IndentXinlangZbType getXinlangZbTypeById(Integer id){
		return (IndentXinlangZbType) indentDao.getObject(IndentXinlangZbType.class, id);
	}
	
	public IndentQqPlzb saveIndentQqPlzb(IndentQqPlzb indentQqPlzb){
		return (IndentQqPlzb) indentDao.save(indentQqPlzb);
	}
	
	public IndentXinlangZb saveIndentXinlangZb(IndentXinlangZb indentXinlangZb){
		return (IndentXinlangZb) indentDao.save(indentXinlangZb);
	}
	
	public Object saveObject(Object t){
		return indentDao.save(t);
	}
	
	public PaginationSupport getIndentForPage(int pageName,boolean flag,List baseList,UserIndentSearch qe,String tableName){
		Session session = indentDao.getSession();
		User cUser = userManagerService.findCurrectUser();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		
		if(flag){
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
//				if(!userManagerService.isRoot(cUser)){
//					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
//					if(t==null) continue;
//				}
				dis.add(Restrictions.eq("type", base));  
			}
			
			criteria.add(dis).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}else{
			
			Disjunction dis=Restrictions.disjunction();
			for(int i=0;i<baseList.size();i++){
				Integer base = (Integer) baseList.get(i);
				if(!userManagerService.isRoot(cUser)){
					UserIndentType t = getUserIndentTypeByUserAndType(cUser, base);
					if(t==null) continue;
				}
				dis.add(Restrictions.eq("type", base));  
			}
			
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(dis).add(Restrictions.eq("deleted", 0)).addOrder(
					Order.desc("createDate"));
		}
		
		if(userManagerService.isAudit(cUser)){
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getParentUserId()+":",MatchMode.ANYWHERE));
		}else{
			criteria.createCriteria("user").add(Restrictions.like("parentUserIdString", ":"+cUser.getUserId().toString()+":",MatchMode.ANYWHERE));
		}
		
if(qe!=null){
			
			if(qe.getBianhao()!=null&&!qe.getBianhao().isEmpty()){
				criteria = criteria.add(Restrictions.eq("id", new Integer(qe.getBianhao())));
			}
			
			if(qe.getWeiboUrl()!=null &&!qe.getWeiboUrl().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getWeiboUrl(),MatchMode.ANYWHERE));
			}
			
			if(qe.getUid()!=null && !qe.getUid().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getUid(),MatchMode.ANYWHERE));
			}
			if(qe.getWeixinNumber()!=null && !qe.getWeixinNumber().isEmpty()){
				criteria = criteria.add(Restrictions.like("weixinNum", qe.getWeixinNumber(),MatchMode.ANYWHERE));
			}
			
			
			
			
		}
		criteria.addOrder(
				Order.desc("createDate"));
		if(qe!=null){
			if(qe.getWeiboId()!=null && !qe.getWeiboId().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("weiboId", qe.getWeiboId(),MatchMode.ANYWHERE));
			}
			if(qe.getName()!=null && !qe.getName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("name", qe.getName(),MatchMode.ANYWHERE));
			}
			if(qe.getTrueName()!=null && !qe.getTrueName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("trueName", qe.getTrueName(),MatchMode.ANYWHERE));
			}
			if(qe.getIdCard()!=null && !qe.getIdCard().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("idCard", qe.getIdCard(),MatchMode.ANYWHERE));
			}
			if(qe.getTel()!=null && !qe.getTel().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("telNumber", qe.getTel(),MatchMode.ANYWHERE));
			}
			if(qe.getBusinessName()!=null && !qe.getBusinessName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("businessName", qe.getBusinessName(),MatchMode.ANYWHERE));
			}
			if(qe.getComName()!=null && !qe.getComName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("remark", qe.getComName(),MatchMode.ANYWHERE));
			}
			
			
		}
		
		PaginationSupport ps = new PaginationSupport();
		
		ProjectionList projectionList = Projections.projectionList();
    	projectionList.add(Projections.groupProperty("state"));
    	projectionList.add(Projections.rowCount());
    	criteria.setProjection(projectionList);
    	List indents = criteria.list();
    	Iterator iterator = indents.iterator();
    	while(iterator.hasNext()) {
    	    Object[] o = (Object[]) iterator.next();
    	    if(o[0].equals(0)){
    	    	ps.setShenheCount((Long)o[1]);
    	    }else if(o[0].equals(1)){
    	    	ps.setZhixingCount((Long)o[1]);
    	    }else if(o[0].equals(2)){
    	    	ps.setWanchengCount((Long)o[1]);
    	    }else if(o[0].equals(3)){
    	    	ps.setTuikuanCount((Long)o[1]);
    	    }else if(o[0].equals(4)){
    	    	ps.setStateCount4((Long)o[1]);
    	    }else if(o[0].equals(5)){
    	    	ps.setStateCount5((Long)o[1]);
    	    }else if(o[0].equals(6)){
    	    	ps.setStateCount6((Long)o[1]);
    	    }
    	}
    	
    	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    	criteria.setProjection(null);
	
    	if(qe!=null){
			
			if (qe.getStatus()!= null&&!qe.getStatus().equalsIgnoreCase("all")) {
				criteria = criteria.add(Restrictions.eq("state", new Integer(qe.getStatus())));
			}
    	}
    	
    	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
    	ps.setItems(ps2.getItems());
    	ps.setIndexes(ps2.getIndexes());
    	ps.setPageNum(ps2.getPageNum());
    	ps.setPageSize(ps2.getPageSize());
    	ps.setStartIndex(ps2.getStartIndex());
    	ps.setTotalCount(totalCount);
	return ps;
		
	}
	
	public PaginationSupport getIndentForPage(int pageName,boolean flag,int base,UserIndentSearch qe,String tableName) {
		Session session = indentDao.getSession();
		Integer userId = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		Criteria criteria = session.createCriteria(UserIndent.class);
		if(flag){
			criteria.add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0));
		}else{
			criteria.add(Restrictions.eq("user.userId", userId)).
					add(Restrictions.eq("type", base)).add(Restrictions.eq("deleted", 0));
		}
		
		if(qe!=null){
			
			if(qe.getBianhao()!=null&&!qe.getBianhao().isEmpty()){
				criteria = criteria.add(Restrictions.eq("id", new Integer(qe.getBianhao())));
			}
			
			if(qe.getWeiboUrl()!=null &&!qe.getWeiboUrl().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getWeiboUrl(),MatchMode.ANYWHERE));
			}
			
			if(qe.getUid()!=null && !qe.getUid().isEmpty()){
				criteria = criteria.add(Restrictions.like("targetUrl", qe.getUid(),MatchMode.ANYWHERE));
			}
			if(qe.getWeixinNumber()!=null && !qe.getWeixinNumber().isEmpty()){
				criteria = criteria.add(Restrictions.like("weixinNum", qe.getWeixinNumber(),MatchMode.ANYWHERE));
			}
			
		}
		criteria.addOrder(
				Order.desc("createDate"));
		if(qe!=null){
			if(qe.getWeiboId()!=null && !qe.getWeiboId().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("weiboId", qe.getWeiboId(),MatchMode.ANYWHERE));
			}
			if(qe.getName()!=null && !qe.getName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("name", qe.getName(),MatchMode.ANYWHERE));
			}
			if(qe.getTrueName()!=null && !qe.getTrueName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("trueName", qe.getTrueName(),MatchMode.ANYWHERE));
			}
			if(qe.getIdCard()!=null && !qe.getIdCard().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("idCard", qe.getIdCard(),MatchMode.ANYWHERE));
			}
			if(qe.getTel()!=null && !qe.getTel().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("telNumber", qe.getTel(),MatchMode.ANYWHERE));
			}
			if(qe.getBusinessName()!=null && !qe.getBusinessName().isEmpty()){
				criteria = criteria.createCriteria("renzheng").createCriteria(tableName).add(Restrictions.like("businessName", qe.getBusinessName(),MatchMode.ANYWHERE));
			}
			
		}
		
		PaginationSupport ps = new PaginationSupport();
		
		ProjectionList projectionList = Projections.projectionList();
    	projectionList.add(Projections.groupProperty("state"));
    	projectionList.add(Projections.rowCount());
    	criteria.setProjection(projectionList);
    	List indents = criteria.list();
    	Iterator iterator = indents.iterator();
    	while(iterator.hasNext()) {
    	    Object[] o = (Object[]) iterator.next();
    	    if(o[0].equals(0)){
    	    	ps.setShenheCount((Long)o[1]);
    	    }else if(o[0].equals(1)){
    	    	ps.setZhixingCount((Long)o[1]);
    	    }else if(o[0].equals(2)){
    	    	ps.setWanchengCount((Long)o[1]);
    	    }else if(o[0].equals(3)){
    	    	ps.setTuikuanCount((Long)o[1]);
    	    }else if(o[0].equals(4)){
    	    	ps.setStateCount4((Long)o[1]);
    	    }else if(o[0].equals(5)){
    	    	ps.setStateCount5((Long)o[1]);
    	    }else if(o[0].equals(6)){
    	    	ps.setStateCount6((Long)o[1]);
    	    }
    	}
    	
    	long totalCount = ((Long) criteria.setProjection(Projections.rowCount()).uniqueResult()).longValue();
    	criteria.setProjection(null);
	
    	if(qe!=null){
			
			if (qe.getStatus()!= null&&!qe.getStatus().equalsIgnoreCase("all")) {
				criteria = criteria.add(Restrictions.eq("state", new Integer(qe.getStatus())));
			}
    	}
    	
    	PaginationSupport ps2 = abstractManager.findPageByCriteria(criteria, pageName);
    	ps.setItems(ps2.getItems());
    	ps.setIndexes(ps2.getIndexes());
    	ps.setPageNum(ps2.getPageNum());
    	ps.setPageSize(ps2.getPageSize());
    	ps.setStartIndex(ps2.getStartIndex());
    	ps.setTotalCount(totalCount);
	return ps;
		
		
	}
	
	public String saveIndentForTaobaoShoucang(UserIndent userIndent,User curUser,String base,UserBaseIndentType baseIndentType){
		userIndent.setCreateDate(new Date());
		userIndent.setNowNumber(userIndent.getStartNumber());
		User parentUser = userManagerService.findUserById(curUser.getParentUserId());
		userIndent.setParentUser(parentUser);
		userIndent.setState(0);
		
		Float price = null;
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		
		userIndent.setType(new Integer(base));
		userIndent.setUser(curUser);
		Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
		userIndent.setTotalPrice(t);
		userIndent.setPrice(price);
		
		//判余额是否充足
		Float money = curUser.getMoney();
		Float money2 = (float) Math.round(money*100/100);
		if(money2<t){
			return "moneyLow";
		}
		
		userIndent.setBasePrice(baseIndentType.getPrice());
		Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
		userIndent.setBaseTotalPrice(baseTotalPrice);
		
		userIndent.setBaseIndentType(baseIndentType);
		//保存订单表
		userIndent = saveUserIndent(userIndent);
		
		//所需金额保存到临时表
		UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
		userIndentMoneyTmp.setMoney(t);
		userIndentMoneyTmp.setUser(curUser);
		userIndentMoneyTmp.setUserIndent(userIndent);
		userIndentMoneyTmp = saveUserIndentMoneyTmp(userIndentMoneyTmp);
		//从用户账户中的总金额减去订单总价 保存
		money = curUser.getMoney();
		money = money -t;
		curUser.setMoney(money);
		UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
		
		//保存返利关系
		UserIndentType indentType = getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
		saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
		//userIndent2 = userIndent;
		userIndent=null;
		
	    return "success";
	}
	
	public String saveIndentForQq(UserIndent userIndent,User curUser,String base,String qq,UserBaseIndentType baseIndentType){

		userIndent.setCreateDate(new Date());
		userIndent.setNowNumber(userIndent.getStartNumber());
		User parentUser = userManagerService.findUserById(curUser.getParentUserId());
		userIndent.setParentUser(parentUser);
		userIndent.setState(0);
		
		Float price = null;
		if(userManagerService.isRoot(curUser)){
			price = baseIndentType.getPrice();
		}else{
			UserIndentType indentType = getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
			if(indentType==null){
				return "403";
			}else{
				indentType = updateIndentType(indentType);
				price = indentType.getPrice();
			}
		}
		
		userIndent.setType(new Integer(base));
		userIndent.setUser(curUser);
		Float t = price*userIndent.getNumber()/baseIndentType.getUnit2();;
		userIndent.setTotalPrice(t);
		userIndent.setPrice(price);
		//判余额是否充足
		Float money = curUser.getMoney();
		Float money2 = (float) Math.round(money*100/100);
		if(money2<t){
			return "moneyLow";
		}
		userIndent.setBasePrice(baseIndentType.getPrice());
		Float baseTotalPrice = baseIndentType.getPrice()*userIndent.getNumber()/baseIndentType.getUnit2();
		userIndent.setBaseTotalPrice(baseTotalPrice);
		
		userIndent.setBaseIndentType(baseIndentType);
		//保存订单表
		userIndent = saveUserIndent(userIndent);
		
		//保存腾讯微博订单对应QQ号码
		
		saveIndentQq(qq,userIndent);
		
		//所需金额保存到临时表
		UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
		userIndentMoneyTmp.setMoney(t);
		userIndentMoneyTmp.setUser(curUser);
		userIndentMoneyTmp.setUserIndent(userIndent);
		userIndentMoneyTmp = saveUserIndentMoneyTmp(userIndentMoneyTmp);
		//从用户账户中的总金额减去订单总价 保存
		money = curUser.getMoney();
		money = money -t;
		curUser.setMoney(money);
		UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
		
		//保存返利关系
		UserIndentType indentType = getUserIndentTypeByUserAndType(curUser,baseIndentType.getId());
		saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
		//userIndent2 = userIndent;
		userIndent=null;
		
	    return "success";
	}
	
	public void saveXinlangPersionRenzheng(XinlangPersonRenzheng xinlangPersonRenzheng,
			UserIndent userIndent,UserBaseIndentType baseIndentType,User curUser,
			UserIndentType indentType,Float t,Float price){
		saveObject(xinlangPersonRenzheng);
		saveObject(userIndent.getRenzheng());
		userIndent.setNumber(1);
		//保存订单表
		userIndent = saveUserIndent(userIndent);
		
		// weiboIndentService.saveIndentQq(qq,userIndent);
		
		//所需金额保存到临时表
		UserIndentMoneyTmp userIndentMoneyTmp = new UserIndentMoneyTmp();
		userIndentMoneyTmp.setMoney(t);
		userIndentMoneyTmp.setUser(curUser);
		userIndentMoneyTmp.setUserIndent(userIndent);
		userIndentMoneyTmp = saveUserIndentMoneyTmp(userIndentMoneyTmp);
		//从用户账户中的总金额减去订单总价 保存
		Float money = curUser.getMoney();
		money = money -t;
		curUser.setMoney(money);
		UserPay userPay = userManagerService.saveUserMoneyForIndent(curUser,0,t,userIndent);
		
		//保存返利关系
		saveUserIndentRebateRelation(userIndent,baseIndentType,0,indentType,curUser,price,null,null);
		//userIndent2 = userIndent;
		userIndent=null;
	}
}

